#!/usr/bin/env python
# -*- coding: utf-8 -*-

from code import Code

""" 構文木背景描画命令クラス """
class DrawBg:
	def __init__(self):
		pass
	def initialize(self, paramList):
		self.paramList = paramList
		self.count = len(self.paramList)
		if self.count == 0:
			raise "Error: DrawBg() invalid parameter count. [%d]"%self.count
	def run(self, writer):
		# 命令書き込み
		# [opecode, type, color or id, effectId]
		# [1      , 1,    4,           1       ]
		# = 7byte
		writer.writeByte(Code.CODE_DRAW_BG)
		
		color = self.paramList[0]
		param = color.getValue()
		try:
			id    = int(param)
			type  = Code.DRAW_TYPE_ID
			typed = 0
			writer.writeByte(type)
			writer.writeLong(id)
		except:
			type  = Code.DRAW_TYPE_COLOR
			typed = 1
			id    = color.getBinaryString()
			writer.writeByte(type)     # 1byte
			writer.writeString(id)     # 3byte
			writer.writeString("\xff") # 1byte
			id = param[1:]
		
		if self.count == 1:
			# デフォルト引数
			effectId = Code.EF_NORMAL
		else:
			effectId = self.paramList[1].getValue()
		writer.writeByte(effectId)
		writer.writeLog("背景描画, %s, %r, %r"%(typed, id, effectId))
	def getInstance():
		""" インスタンスの生成 """
		return DrawBg()
	getInstance = staticmethod(getInstance)